﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace RMSM
{
	[Serializable()]
	public class ViewDUTonKhoKhoaChiTietBO : Csla.BusinessBase<ViewDUTonKhoKhoaChiTietBO>
	{
		#region Business Properties
		
private int? _DUTonKhoKhoaChiTietID = null;
public int? DUTonKhoKhoaChiTietID
{
get { return this._DUTonKhoKhoaChiTietID; }
set { this._DUTonKhoKhoaChiTietID = value; PropertyHasChanged("DUTonKhoKhoaChiTietID"); }
}

private int? _TonKhoKhoaID = null;
public int? TonKhoKhoaID
{
get { return this._TonKhoKhoaID; }
set { this._TonKhoKhoaID = value; PropertyHasChanged("TonKhoKhoaID"); }
}

private int? _KhoaID = null;
public int? KhoaID
{
    get { return this._KhoaID; }
    set { this._KhoaID = value; PropertyHasChanged("KhoaID"); }
}

private string _TenKho = null;
public string TenKho
{
    get { return this._TenKho; }
    set { this._TenKho = value; PropertyHasChanged("TenKho"); }
}

private int? _ThuocID = null;
public int? ThuocID
{
get { return this._ThuocID; }
set { this._ThuocID = value; PropertyHasChanged("ThuocID"); }
}

private string _TenThuoc = null;
public string TenThuoc
{
    get { return this._TenThuoc; }
    set { this._TenThuoc = value; PropertyHasChanged("TenThuoc"); }
}

private int? _DonVTID = null;
public int? DonVTID
{
get { return this._DonVTID; }
set { this._DonVTID = value; PropertyHasChanged("DonVTID"); }
}

private string _TenDonVT = null;
public string TenDonVT
{
    get { return this._TenDonVT; }
    set { this._TenDonVT = value; PropertyHasChanged("TenDonVT"); }
}

private string _MaThuoc = null;
public string MaThuoc
{
get { return this._MaThuoc; }
set { this._MaThuoc = value; PropertyHasChanged("MaThuoc"); }
}

private double? _GiaMua = null;
public double? GiaMua
{
get { return this._GiaMua; }
set { this._GiaMua = value; PropertyHasChanged("GiaMua"); }
}

private double? _GiaBan = null;
public double? GiaBan
{
    get { return this._GiaBan; }
    set { this._GiaBan = value; PropertyHasChanged("GiaBan"); }
}

private double? _SoLuongTon = null;
public double? SoLuongTon
{
    get { return this._SoLuongTon; }
    set { this._SoLuongTon = value; PropertyHasChanged("SoLuongTon"); }
}

private DateTime? _NgayHetHan = null;
public DateTime? NgayHetHan
{
get { return this._NgayHetHan; }
set { this._NgayHetHan = value; PropertyHasChanged("NgayHetHan"); }
}

private int? _UserCreated = Session.LoginID;
public int? UserCreated
{
get { return this._UserCreated; }
set { this._UserCreated = value; PropertyHasChanged("UserCreated"); }
}

private DateTime? _DateCreated = DateTime.MaxValue;
public DateTime? DateCreated
{
get { return this._DateCreated; }
set { this._DateCreated = value; PropertyHasChanged("DateCreated"); }
}

private int? _UserModify = null;
public int? UserModify
{
get { return this._UserModify; }
set { this._UserModify = value; PropertyHasChanged("UserModify"); }
}

private DateTime? _DateModify = null;
public DateTime? DateModify
{
get { return this._DateModify; }
set { this._DateModify = value; PropertyHasChanged("DateModify"); }
}

private bool? _Deleted = false;
public bool? Deleted
{
get { return this._Deleted; }
set { this._Deleted = value; PropertyHasChanged("Deleted"); }
}

		private int? _STT0 = 0;
		public int? STT0
		{
			get { return _STT0; }
			set { _STT0 = value; }
		}
		
		protected override object GetIdValue()
		{
			return _DUTonKhoKhoaChiTietID.Value;
		}
		#endregion Business Properties
		//==========================================================================
		
		#region Factory Methods
		public void SetIsNew()
		{
			MarkNew();
		}

		public void SetIsOld()
		{
			MarkOld();
		}
		
		public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }
		
		private static int newDUTonKhoKhoaChiTietID =  -1;
private ViewDUTonKhoKhoaChiTietBO()
{
_DUTonKhoKhoaChiTietID = newDUTonKhoKhoaChiTietID--;
}

		public ViewDUTonKhoKhoaChiTietBO(SqlDataReader dataReader)
		{
			MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public ViewDUTonKhoKhoaChiTietBO(SqlDataReader dataReader, bool isRoot)
		{
			if (!isRoot)
                MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public static ViewDUTonKhoKhoaChiTietBO NewRootViewDUTonKhoKhoaChiTietBO()
		{
			return DataPortal.Create<ViewDUTonKhoKhoaChiTietBO>(new RootCriteria());
		}

		internal static ViewDUTonKhoKhoaChiTietBO NewChildViewDUTonKhoKhoaChiTietBO()
		{
			return DataPortal.Create<ViewDUTonKhoKhoaChiTietBO>(new ChildCriteria());
		}

		public static ViewDUTonKhoKhoaChiTietBO GetRootViewDUTonKhoKhoaChiTietBO(int _DUTonKhoKhoaChiTietID)
		{
			return DataPortal.Fetch<ViewDUTonKhoKhoaChiTietBO>(new RootCriteria(_DUTonKhoKhoaChiTietID));
		}

		public static ViewDUTonKhoKhoaChiTietBO GetRootViewDUTonKhoKhoaChiTietBOByWhere(string where)
		{
			return DataPortal.Fetch<ViewDUTonKhoKhoaChiTietBO>(where);
		}

		internal static ViewDUTonKhoKhoaChiTietBO GetChildViewDUTonKhoKhoaChiTietBO(SqlDataReader dataReader)
		{
			return new ViewDUTonKhoKhoaChiTietBO(dataReader);
		}

		public static void DeleteViewDUTonKhoKhoaChiTietBO(int _DUTonKhoKhoaChiTietID)
		{
			DataPortal.Delete(new RootCriteria(_DUTonKhoKhoaChiTietID));
		}

		public static void DeleteViewDUTonKhoKhoaChiTietBO(int _DUTonKhoKhoaChiTietID, bool directDelete)
		{
			DataPortal.Delete(new RootCriteria(_DUTonKhoKhoaChiTietID, directDelete));
		}
		#endregion Factory Methods
		//==========================================================================
		
		#region Criteria
		[Serializable()]
		public class RootCriteria
		{
			public int DUTonKhoKhoaChiTietID;
			public bool directDelete = false;
			public RootCriteria(int _DUTonKhoKhoaChiTietID)
			{
				this.DUTonKhoKhoaChiTietID = _DUTonKhoKhoaChiTietID;
				this.directDelete = false;
			}
			
			public RootCriteria(int _DUTonKhoKhoaChiTietID, bool directDelete)
            {
                this.DUTonKhoKhoaChiTietID = _DUTonKhoKhoaChiTietID;
                this.directDelete = directDelete;
            }
            
			public RootCriteria() {}
		}

		[Serializable()]
		public class ChildCriteria
		{
			public int DUTonKhoKhoaChiTietID;
			public bool directDelete = false;
			public ChildCriteria(int _DUTonKhoKhoaChiTietID)
			{
				this.DUTonKhoKhoaChiTietID = _DUTonKhoKhoaChiTietID;
				this.directDelete = false;
			}
			
			public ChildCriteria(int _DUTonKhoKhoaChiTietID, bool directDelete)
            {
                this.DUTonKhoKhoaChiTietID = _DUTonKhoKhoaChiTietID;
                this.directDelete = directDelete;
            }
            
			public ChildCriteria() {}
		}
		#endregion Criteria
		//==========================================================================

		#region Create
		[RunLocal]
		private void DataPortal_Create(RootCriteria criteria)
		{
		}

		private void DataPortal_Create(ChildCriteria criteria)
		{
			MarkAsChild();
			ValidationRules.CheckRules();
		}

		#endregion Create
		//==========================================================================

		#region Fetch
		[Transactional(TransactionalTypes.TransactionScope)]
		protected void DataPortal_Fetch(RootCriteria criteria)
		{
			this.Execute_Fetch(criteria);
		}

		protected void DataPortal_Fetch(string where)
		{
			this.Execute_Fetch(where);
		}

		protected void Execute_Fetch(RootCriteria criteria)
		{
			this.This_Fetch(criteria);
		}
		
		protected void Execute_Fetch(string where)
		{
			this.This_Fetch(where);
		}

		private void This_Fetch(RootCriteria criteria)
		{
			
		}

		private void This_Fetch(string where)
		{
			
		}

		private void Fetch(SqlDataReader dataReader)
		{
			

		}
		#endregion Fetch
		//==========================================================================

		#region Insert
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Insert()
		{
			this.Execute_Insert();
		}

		protected void Execute_Insert()
		{
			this.This_Insert();
		}

		private void This_Insert()
		{
			
		}
		#endregion Insert
		//==========================================================================

		#region Update
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Update()
		{
			this.Execute_Update();
		}

		protected void Execute_Update()
		{
			this.This_Update();
		}

		private void This_Update()
		{
			
		}
		#endregion Update
		//==========================================================================

		#region Delete
		[Transactional(TransactionalTypes.TransactionScope)]
		protected void DataPortal_DeleteSelf()
		{
			DataPortal_Delete(new RootCriteria(_DUTonKhoKhoaChiTietID.Value));
		}

		private void DataPortal_Delete(RootCriteria criteria)
		{
			this.Execute_Delete(criteria);
		}

		protected void Execute_Delete(RootCriteria criteria)
		{
			
		}
		#endregion Delete
		//==========================================================================

		#region Internal method

        public void CopyFromDUTonKhoKhoaChiTietBO(DUTonKhoKhoaChiTietBO hdbBO)
        {
            this.DUTonKhoKhoaChiTietID = hdbBO.DUTonKhoKhoaChiTietID;
            TonKhoKhoaID = hdbBO.TonKhoKhoaID;
            KhoaID = DUTonKhoKhoaBO.GetRootDUTonKhoKhoaBO(StaticUtil.GetInt(TonKhoKhoaID,-1)).KhoaID;
            TenKho = DMKhoaBO.GetRootDMKhoaBO(StaticUtil.GetInt(KhoaID, -1)).NoiDung;
            ThuocID = hdbBO.ThuocID;
            TenThuoc = DUThuocBO.GetRootDUThuocBO(StaticUtil.GetInt(ThuocID, -1)).Ten;
            DonVTID = hdbBO.DonVTID;
            TenDonVT = DUDonVTBO.GetRootDUDonVTBO(StaticUtil.GetInt(DonVTID, -1)).Ten;
            MaThuoc = hdbBO.MaThuoc;
            SoLuongTon = StaticUtil.GetDouble(hdbBO.SoLuongTon, 0);
            NgayHetHan = StaticUtil.GetDateTime(hdbBO.NgayHetHan, DateTime.Now);

            DUGiaThuocBO tempGiaThuoc = DUGiaThuocBO.GetRootDUGiaThuocBOByWhere("ThuocID = " + ThuocID.ToString() + " and DonViTinhID = " + DonVTID.ToString());
            if (StaticUtil.GetInt(tempGiaThuoc.DUGiaThuocID, -1) < 0)
            {
                GiaMua = GiaBan = 0;
            }
            else
            {
                GiaMua = StaticUtil.GetDouble(tempGiaThuoc.GiaMua, 0);
                GiaBan = StaticUtil.GetDouble(tempGiaThuoc.GiaBan, 0);
            }
        }

		internal void Insert()
		{
			if (!this.IsDirty) return;
			DataPortal_Insert();
			MarkOld();
		}

		internal void Update()
		{
			if (!this.IsDirty) return;
			DataPortal_Update();
			MarkOld();
		}

		internal void DeleteSelf()
		{
			if (!this.IsDirty) return;
			if (this.IsNew) return;
			DataPortal_Delete(new RootCriteria(_DUTonKhoKhoaChiTietID.Value));
			MarkNew();
		}
		#endregion Internal method
		//==========================================================================
	}
}
